package com.spatio.serving.basic.service.template.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spatio.serving.basic.service.template.common.domain.mutliFoodRecordDo;
import com.spatio.serving.basic.service.template.common.domain.singleFoodRecordDo;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnswerProcessUtils {

    public String getAnswerText(String answer){
        String regex = "\\{.*\\}";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(answer);
        int startIndex = answer.length();
        if(matcher.find()){
            startIndex = matcher.start();
        }
        String nonJsonBefore = answer.substring(0, startIndex).trim();
        return nonJsonBefore;
    }

    public JSONArray getAnswerJson(String answer){
        String regex = "\\{.*\\}";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(answer);

        JSONArray temp_food= new JSONArray();

        if (matcher.find()) {
            String jsonString = matcher.group();
            try {
                // 解析 JSON 数据
                JSONObject jsonObject = JSON.parseObject(jsonString);
                //System.out.println(jsonObject.toString(4));
                JSONArray foodNameArray = jsonObject.getJSONArray("food_name");
                for(int i = 0;i<foodNameArray.size();i++){
                    String record_id=String.valueOf(UUID.randomUUID());
                    //singleFoodRecordDo single = new singleFoodRecordDo();
                    JSONObject jsonObject1 = (JSONObject) jsonObject.get(foodNameArray.getString(i));
                    JSONArray allIngredientsArray = jsonObject1.getJSONArray("all_Ingredients");
                    for(int j = 0;j<allIngredientsArray.size();j++){
                        mutliFoodRecordDo mutli = new mutliFoodRecordDo();
                        String uuid = String.valueOf(UUID.randomUUID());
                        mutli.setUuid(uuid);
                        mutli.setRecord_id(record_id);
                        mutli.setRoot_food(foodNameArray.getString(i));
                        mutli.setSingle_food(allIngredientsArray.getString(j));
                        JSONObject jsonObject2 = (JSONObject) jsonObject1.get(allIngredientsArray.getString(j));
                        //alljson.put(allIngredientsArray.getString(j),jsonObject2);
                        mutli.setCalories(Double.valueOf(jsonObject2.getString("calories")));
                        mutli.setFats(Double.valueOf(jsonObject2.getString("fat")));
                        mutli.setProteins(Double.valueOf(jsonObject2.getString("protein")));
                        mutli.setCarbs(Double.valueOf(jsonObject2.getString("carbohydrates")));
                        mutli.setWeights(Double.valueOf(jsonObject2.getString("weight")));
                        temp_food.add(mutli);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } else {
            System.out.println("未找到 JSON ");
            return null;
        }
        return temp_food;
    }
}
